<?php

namespace app\common\service;

use app\common\service\Base;
use think\Db;

/**
 * 成本管理》核价跟进
 */
class PriceDiscuss extends Base
{

    /**
     * 自动添加审核数据
     * 面辅二次工艺名称 为 染色 的 订单数据插入
     *
     * //wx_price_discuss表数据更新
     * (new \app\common\service\PriceDiscuss())->order_sync_data($orderId);
     * @param $param
     * @date 2021/12/9
     * @author zhuguosen 905502314@qq.com
     */
    public function order_sync_data($orderId)
    {
        //1,判断数据是否存在 wx_price_discuss表  面辅二次工艺名称 为 染色 的 订单数据
        $info = db('price_discuss pdis')->where([
            ['pdis.order_id', '=', $orderId],
            ['pdis.type', '=', 'special_process_a_name_yanse'],
        ])->find();
        if (isset($info['id'])) {
            return true;
        }
        //2,不存在就数据插入
        $orderInfo = db('order o')
            ->field('o.id,o.client_ordernum 
                ,ext.special_process_a_name,ext.special_process_a_unitprice ')
            ->join('order_ext ext', 'o.id = ext.order_id', 'left')
            ->where([
                ['o.id', '=', $orderId],
                ['ext.special_process_a_name', '=', '染色'],
            ])->find();
        if (!isset($orderInfo['id'])) {
            return true;
        }
        $temp = db('price_discuss')->insert([
            'order_id' => $orderId,
            'client_ordernum' => $orderInfo['client_ordernum'],
            'type' => 'special_process_a_name_yanse',
            'status' => 0,
            'special_process_a_name' => trim($orderInfo['special_process_a_name']),
            'special_process_a_unitprice' => floatval($orderInfo['special_process_a_unitprice']),
            'price_0' => 0,
            'price_1' => 0,
            'admin_id' => 0,
            'create_time' => time(),
            'update_time' => time(),
        ]);
        return $temp;
    }

    /**
     * 定时运行数据同步
     * 1，special_process_a_name=染色 并且
     * 2，在裁剪时，修改成可审核状态
     * 插入数据
     * @date 2021/12/9
     * @author zhuguosen 905502314@qq.com
     */
    public function order_sync_all()
    {
        $page = 1;
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
                ['ext.special_process_a_name', '=', '染色'],
            ];
            $where[] = ['ml.type', '=', 'fabric'];
            $where[] = ['om.caichuang_can_do', '=', 1];//面料可裁就是 裁剪中
            $where[] = ['om.caichuang_pull_status', '=', 0];//裁床拉布状态（0未确认，1已确认） 0就是没完成  1就是裁床完成了

            $list = db('order o')
                ->field('o.id,o.client_ordernum 
                ,ext.special_process_a_name,ext.special_process_a_unitprice 
                ,pdis.id as pdis_id,pdis.status ')
                ->join('order_ext ext', 'o.id = ext.order_id', 'left')
                ->join('price_discuss pdis', 'o.id = pdis.order_id', 'left')
                ->join('wx_order_material om ', 'om.order_id=o.id', 'left')
                ->join('wx_material ml ', 'om.material_id=ml.id', 'left')
                ->where($where)->page($page)->limit(10)
                ->group('om.order_id')
                ->select();
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($list as $k => $v) {
                if (intval($v['pdis_id']) == 0) {
                    $res = $this->order_sync_one($v);
                }
            }
        }
    }

    public function order_sync_one($orderInfo)
    {
        $temp = db('price_discuss')->insert([
            'order_id' => $orderInfo['id'],
            'client_ordernum' => $orderInfo['client_ordernum'],
            'type' => 'special_process_a_name_yanse',
            'status' => 1,
            'special_process_a_name' => trim($orderInfo['special_process_a_name']),
            'special_process_a_unitprice' => floatval($orderInfo['special_process_a_unitprice']),
            'price_0' => 0,
            'price_1' => 0,
            'admin_id' => 0,
            'create_time' => time(),
            'update_time' => time(),
        ]);
        return $temp;
    }
}